package com.artollkitplus.benchmark;

import java.util.Vector;

public class DebugFps {
	public static int _fps = 0;
	public static int _avg_fps = 0;
	private static int _fps_sum = 0;
	private static int	_fps_count;	
	private static long	_last_fps_tick;
	private static Vector<Integer> fps_history = new Vector<Integer>();
	////
	private static long lastTime = System.currentTimeMillis();
	public static long currFrameTime = 0;
	private static Vector<Long> currFrameTimeHistory = new Vector<Long>();
	public static int avgFrameTime = 0;
	private static int sumFrameTime = 0;
	////
	public static void updateFPS(){
		long tickFrameTime = System.currentTimeMillis();
		currFrameTime = tickFrameTime - lastTime;
		lastTime = tickFrameTime;
		currFrameTimeHistory.add(currFrameTime);
		if(currFrameTimeHistory.size()>20){
			currFrameTimeHistory.remove(0);
		}
		for(int i = 0;i<currFrameTimeHistory.size();i++){
			sumFrameTime += currFrameTimeHistory.elementAt(i);
		}
		avgFrameTime = sumFrameTime/currFrameTimeHistory.size();
		sumFrameTime = 0;
		///
		if (CurrentConfig.SHOW_FPS)
		{
			_fps_count++;
			long tick = System.currentTimeMillis();
			if (tick - _last_fps_tick > 1000)
			{
				_fps = (_fps_count * 1000) / (int)(tick - _last_fps_tick);
				_last_fps_tick = tick;
				_fps_count = 0;
				////
				fps_history.add(_fps);
				if(fps_history.size()>20){
					fps_history.remove(0);
				}
				for(int i = 0;i<fps_history.size();i++){
					_fps_sum += fps_history.elementAt(i);
				}
				_avg_fps = _fps_sum/fps_history.size();
				_fps_sum = 0;
			}
		}
	}
	
	public static void resetFPS(){
		_last_fps_tick=System.currentTimeMillis();
		fps_history = new Vector<Integer>();
		_avg_fps = 0;
		_fps_sum = 0;
	}
}
